Completed
Push — master ( 60728d...8a100d )
by Andres
36s
created

dark.js ➔ dark   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 88
rs 8.6012
nop 6

7 Functions

Rating   Name   Duplication   Size   Complexity  
C dark.js ➔ ... ➔ ct.darkPrestige 0 28 7
A dark.js ➔ ... ➔ ct.buyDarkUpgrade 0 11 1
A dark.js ➔ ... ➔ refresh 0 12 4
A dark.js ➔ ... ➔ ct.darkProduction 0 8 2
A dark.js ➔ ... ➔ ct.update 0 3 1
A dark.js ➔ ... ➔ ct.visibleDarkUpgrades 0 3 1
A dark.js ➔ ... ➔ isDarkUpgradeVisible 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 dark
3
 Component that handles dark matter and second prestige logic.
4
 It handles dark matter production and dark upgrades.
5
 A second prestige resets the progress of all elements with exotic
6
 matter and produces dark matter.
7
8
 @namespace Components
9
 */
10
'use strict';
11
12
angular.module('game').component('dark', {
13
  templateUrl: 'views/dark.html',
14
  controller: ['state', 'format', 'visibility', 'upgrade', 'data', 'util', dark],
15
  controllerAs: 'ct'
16
});
17
18
function dark(state, format, visibility, upgrade, data, util) {
19
  let ct = this;
20
  ct.state = state;
21
  ct.data = data;
22
  ct.util = util;
23
  ct.format = format;
24
  ct.cache = {breakdown:{}};
25
26
  ct.update = function(player) {
27
    refresh(player);
28
  };
29
30
  /* Refreshes the values in the cache */
31
  function refresh(player){
32
      ct.cache.breakdown = {};
33
      for (let element in data.elements) {
34
        let exotic = data.elements[element].exotic;
35
        if (!player.resources[exotic].unlocked || !player.statistics.dark_run[exotic]) {
36
          continue;
37
        }
38
        let number = player.statistics.dark_run[exotic];
39
        let darkProduction = Math.pow(Math.E,(-0.5+Math.sqrt(0.25+0.8686*Math.log(number/100)))/1.4427) || 0;
40
        ct.cache.breakdown[exotic] = Math.round(Math.max(0, darkProduction));
41
      }
42
  }
43
44
  ct.darkProduction = function() {
45
    let production = 0;
46
    for (let resource in ct.cache.breakdown) {
47
      production += ct.cache.breakdown[resource];
48
    }
49
50
    return production;
51
  };
52
53
  ct.darkPrestige = function() {
54
    let production = ct.darkProduction();
55
56
    util.addResource(state.player, 'all_time', 'dark_matter', production);
57
58
    for(let key in data.elements){
59
      let element = data.elements[key];
60
      state.player.resources[element.exotic].number = 0;
61
      if(!state.player.exotic_upgrades[key]){
62
        continue;
63
      }
64
      for(let up in data.exotic_upgrades){
65
        state.player.exotic_upgrades[key][up] = false;
66
      }
67
    }
68
    for(let slot of state.player.element_slots){
69
      if(!slot){
70
        continue;
71
      }
72
      upgrade.resetElement(state.player, slot.element);
73
    }
74
75
    for(let i in state.player.element_slots){
76
      state.player.element_slots[i] = null;
77
    }
78
    state.player.statistics.exotic_run = {};
79
    state.player.statistics.dark_run = {};
80
  };
81
82
  ct.buyDarkUpgrade = function(name) {
83
    let upgrades = state.player.dark_upgrades;
84
    let price = data.dark_upgrades[name].price;
85
    let currency = 'dark_matter';
86
    upgrade.buyUpgrade(state.player,
87
      upgrades,
88
      data.dark_upgrades[name],
89
      name,
90
      price,
91
      currency);
92
  };
93
94
  ct.visibleDarkUpgrades = function() {
95
    return visibility.visible(data.dark_upgrades, isDarkUpgradeVisible, 0);
96
  };
97
98
  // here we receive not the name of the element, but the index in the element_slots
99
  function isDarkUpgradeVisible(name, index) {
100
    return visibility.isUpgradeVisible(name, index, data.dark_upgrades[name]);
101
  }
102
103
  state.registerUpdate('dark', ct.update);
104
  refresh(state.player);
105
}
106